package com.roy.spring.controller;

import com.roy.spring.entity.User;
import com.roy.spring.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Api(value="/users", tags="测试接口模块")
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    UserService userService;

    @ApiOperation(value="获取用户列表", notes="ddddd")
    @GetMapping
    public ResponseEntity<List<User>> find(@RequestParam Map<String,String> parmasMap) throws Exception {
        //params will get all request parameters.
        Map<String, Object> backParams = new HashMap<>();
        for (Map.Entry<String, String> entry : parmasMap.entrySet()) {
            System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue());
            if (entry.getKey().startsWith("_seq_")) {
                backParams.put(entry.getKey(),  entry.getValue());
            }
            if (entry.getKey().startsWith("_dgt_")) {
                Date createTime_gt = new DateFormatter("yyyy-MM-dd").parse(entry.getValue(), Locale.CHINA);
                backParams.put(entry.getKey(), createTime_gt);
            }
            if (entry.getKey().startsWith("_dlt_")) {
                Date createTime_lt = new DateFormatter("yyyy-MM-dd").parse(entry.getValue(), Locale.CHINA);
                backParams.put(entry.getKey(), createTime_lt);
            }
        }
        if (!StringUtils.hasText(parmasMap.get("pageNo"))) {
            backParams.put("pageNo", 0);
        } else {
            backParams.put("pageNo", Integer.parseInt(parmasMap.get("pageNo")));
        }

        if (!StringUtils.hasText(parmasMap.get("pageSize"))) {
            backParams.put("pageSize", 20);
        } else {
            backParams.put("pageSize", Integer.parseInt(parmasMap.get("pageSize")));
        }

        Page<User> page = userService.find(backParams);
        System.out.println("total Elements:" + page.getTotalElements());
        System.out.println("total Pages:" + page.getTotalPages());


        List<User> list = page.getContent();

        return new ResponseEntity<List<User>>(list, null, HttpStatus.OK);
    }

    @ApiOperation(value="获取用户详细信息", notes="根据url的username来获取用户详细信息")
    @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String")
    @GetMapping(path = "/{username}")
    public ResponseEntity<List<User>> findOne(@PathVariable String username) {
        return new ResponseEntity(userService.findByUsername(username),HttpStatus.OK);
    }

}
